package com.acme.ioniolab.copyfile.implementation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.acme.ioniolab.copyfile.FileCopier;

public class IOFileCopier implements FileCopier {

	public void copyFile(String srcFileName, String destFileName) throws IOException {
		File src = new File(srcFileName);
		File dest = new File(destFileName);
		
		if (!src.exists()) {
			throw new IOException("File "+srcFileName+" not found");
		}
		if (!dest.exists()) {
			if (!dest.createNewFile()) {
				throw new IOException("File "+destFileName+" doesn't exist and couldn't be created");
			}
		}
		
		try (FileInputStream inputStream = new FileInputStream(src);
				FileOutputStream outputStream = new FileOutputStream(dest)) {

			int bytesRead = 0;
			byte[] buff = new byte[1024];

			while ((bytesRead = inputStream.read(buff)) != -1) {
				outputStream.write(buff, 0, bytesRead);
			}
		}
	}
}
